package com.guidance.guidance.controller;

import java.awt.print.Pageable;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.guidance.guidance.domain.ActivityInformation;
import com.guidance.guidance.domain.Message;
import com.guidance.guidance.repository.ActivityInformationRepository;

@RestController
@RequestMapping("/activity")
public class CreateActivityInformationController {
	
	@Autowired
	private ActivityInformationRepository aIR;
	
	/**
	 * 发表信息，包含多文件上传
	 * @param msg
	 * @return
	 */
	@RequestMapping("/publishInformation")
	public Message<ActivityInformation> uploadFiles(Message<ActivityInformation> msg,HttpServletRequest request){
		System.out.println(request.getParameter("content"));
		System.out.println(request.getParameter("publishTime"));
		System.out.println(request.getParameter("peopleName"));
		StringBuffer path = new StringBuffer();
		List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("contentPic");
		System.out.println(files.size());
		MultipartFile file =null;
		BufferedOutputStream stream = null;
		for(int i = 0 ; i<files.size() ; ++i) {
			file = files.get(i);
			if(!file.isEmpty()) {
				String saveFileName = file.getOriginalFilename();
				long time = System.currentTimeMillis();
				File saveFile = new File("/usr/local/photo/activity/"+time+saveFileName+".jpg");
				if(!saveFile.getParentFile().exists()) {
					saveFile.getParentFile().mkdirs();
				}
				try {
					byte[] bytes = file.getBytes();
					stream = new BufferedOutputStream(new FileOutputStream(saveFile));
					stream.write(bytes);
					stream.flush();
					stream.close();
					path.append("http://120.78.188.189:8080/GuidanceManagement/activityPhoto/"+time+saveFileName+".jpg"+"&");
					msg.setCode("200");
					msg.setMsg("发表成功");
					msg.setSuccess("true");
				} catch (IOException e) {
					stream = null;
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else {
				msg.setCode("404");
				msg.setMsg("发表失败");
				msg.setSuccess("false");
			}
		}
		if(msg.getMsg().equals("发表成功")){
			ActivityInformation aIF = new ActivityInformation();
//			Date day = new Date();
//			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			aIF.setContent(request.getParameter("content"));
			aIF.setTime(request.getParameter("publishTime"));
			aIF.setUse_photo(request.getParameter("useHeadPhoto"));
			aIF.setName(request.getParameter("peopleName"));
			aIF.setPhoto_path(path.toString());
			aIR.save(aIF);
			System.out.println(path.toString());
			msg.setData(aIF);
		}
		
		return msg;
	}
	
	
	/**
	 * 只发表文字内容的活动信息
	 * @param msg
	 * @return
	 */
	@RequestMapping("/publishText")
	public Message<ActivityInformation> onlyPublishText(Message<ActivityInformation> msg,HttpServletRequest request){
		System.out.println(request.getParameter("publishTime"));
		System.out.println(request.getParameter("peopleName"));
		ActivityInformation aIF = new ActivityInformation();
//		Date day = new Date();
//		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		aIF.setContent(request.getParameter("content"));
		aIF.setTime(request.getParameter("publishTime"));
		aIF.setName(request.getParameter("peopleName"));
		aIF.setUse_photo(request.getParameter("useHeadPhoto"));
		aIR.save(aIF);
		if(aIR.save(aIF) != null) {
			msg.setCode("200");
			msg.setMsg("发表成功");
			msg.setSuccess("true");
			msg.setData(aIF);
		}else {
			msg.setCode("404");
			msg.setMsg("发表失败");
			msg.setSuccess("false");
		}
		return msg;
	}
	
	/**
	 * 获取活动信息
	 * @param msg
	 * @return
	 */
	@RequestMapping("/getActivityInfo")
	public Message<List> getActivityInformation(Message<List> msg){
		List<ActivityInformation> gAI = aIR.findAll();
		if(gAI.size()!=0 && gAI != null) {
			msg.setCode("200");
			msg.setMsg("获取成功");
			msg.setSuccess("true");
			msg.setData(gAI);
		}else {
			msg.setCode("404");
			msg.setMsg("获取失败");
			msg.setSuccess("false");
		}
		return msg;
	}
	
}
